//
//  AchieveMentViewController.m
//  AttendenceSys
//
//  Created by ld on 16/1/14.
//  Copyright © 2016年 窦芳俊. All rights reserved.
//

#import "AchieveMentViewController.h"
#import "ASNetworkEngine.h"
#import "ASAppDelegate.h"
#import "ASAchievementDetailVC.h"
#import "ASMyHonorVC.h"
#import "ASResearchVC.h"

#define kachiTableViewHeaderHeight 10
#define kachiTableViewCellHeight   44

@interface AchieveMentViewController ()

@end

@implementation AchieveMentViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [self initUI];
    [self fetchAchievementType];
}

- (void)initUI
{
    UIImageView* bgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    bgView.backgroundColor = [UIColor whiteColor];
    //bgView.image = [UIImage imageNamed:@"bg_personal"];
    bgView.userInteractionEnabled = YES;
    //[self.view addSubview:bgView];
    
    // achiTableView
    _achiTableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    _achiTableView.frame = CGRectMake(0,
                                      0,
                                      self.view.frame.size.width,
                                      kachiTableViewHeaderHeight * 7 + 7 * kachiTableViewCellHeight);
    _achiTableView.delegate = self;
    _achiTableView.dataSource = self;
    _achiTableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    _achiTableView.backgroundColor = [UIColor clearColor];
    _achiTableView.rowHeight = kachiTableViewCellHeight;
    [self.view addSubview:_achiTableView];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

- (void)fetchAchievementType
{
    //获取用户状态的类型 http://121.42.185.124:8080/LawSchool/getConditionType.action conditonTypeId类型ID conditonTypeName 类型名称
    
    if (_achiTypeArray == nil)
    {
        _achiTypeArray = [[NSMutableArray alloc] init];
    }
    
    NSString *refer = @"getConditionType.action";
    NSString *prefix = kASHttpUrl(refer);
    
    [SVProgressHUD showWithStatus:@"获取中..." maskType:SVProgressHUDMaskTypeClear];
    ASNetworkEngine *networkingEngine = [ASNetworkEngine sharedInstance];

    typeof(self) __weak weakSelf = self;
    [networkingEngine POST:prefix parameters:nil success:^(id responseObject) {
        NSDictionary* jsonRsp = (NSDictionary *)responseObject;
        if (isSuccessRsp(jsonRsp))
        {
            NSArray* data = [jsonRsp objectForKey:kASJsonData];
            [weakSelf parseAchievementType:data];
            [SVProgressHUD dismiss];
            
            [_achiTableView reloadData];
        }
        else
        {
            [SVProgressHUD showErrorWithStatus:errorMsgRsp(jsonRsp)];
        }
    } failure:^(NSError *error) {
        //[SVProgressHUD showErrorWithStatus:error.description];
        [SVProgressHUD showErrorWithStatus:@"网络异常，请稍后再试"];
    }];
}

- (void)parseAchievementType:(NSArray *)array
{
    for (int i = 0; i < [array count]; i++)
    {
        ASAchievementType* type = [[ASAchievementType alloc] init];
        type.typeId = [[[array objectAtIndex:i] objectForKey:@"conditonTypeId"] integerValue];
        type.typeName = [[array objectAtIndex:i] objectForKey:@"conditonTypeName"];
        
        [_achiTypeArray addObject:type];
    }
}

#pragma mark - UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return [_achiTypeArray count];
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return kachiTableViewHeaderHeight;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:@"achiTableViewCellIdentifier"];
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] init];
    }
    
    ASAchievementType* type = [_achiTypeArray objectAtIndex:[indexPath section]];
    cell.textLabel.text = type.typeName;
    
//    if (indexPath.section == 0)
//    {
//        ASAchievementType* type = [_achiTypeArray objectAtIndex:[indexPath row]];
//        cell.textLabel.text = type.typeName;
//    }
//    else if (indexPath.section == 1)
//    {
//        //cell.textLabel.text = @"奖惩";
//        if ([_achiTypeArray count] > 6)
//        {
//            ASAchievementType* type = [_achiTypeArray objectAtIndex:([_achiTypeArray count] - 4)];
//            cell.textLabel.text = type.typeName;
//        }
//    }
//    else if (indexPath.section == 2)
//    {
//        //cell.textLabel.text = @"科研";
//        if ([_achiTypeArray count] > 6)
//        {
//            ASAchievementType* type = [_achiTypeArray objectAtIndex:([_achiTypeArray count] - 3)];
//            cell.textLabel.text = type.typeName;
//        }
//    }
//    else if (indexPath.section == 3)
//    {
//        //cell.textLabel.text = @"其它";
//        if ([_achiTypeArray count] > 6)
//        {
//            ASAchievementType* type = [_achiTypeArray objectAtIndex:([_achiTypeArray count] - 2)];
//            cell.textLabel.text = type.typeName;
//        }
//    }
//    else if (indexPath.section == 4)
//    {
//        //cell.textLabel.text = @"成长轨迹";
//        if ([_achiTypeArray count] > 6)
//        {
//            ASAchievementType* type = [_achiTypeArray objectAtIndex:([_achiTypeArray count] - 1)];
//            cell.textLabel.text = type.typeName;
//        }
//    }
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    
    return cell;
}

#pragma mark - UITableViewDelegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    if ((indexPath.section == 0)
        || (indexPath.section == 1)
        || (indexPath.section == 2)
        || (indexPath.section == 5)
        || (indexPath.section == 6))
    {
        ASAchievementType* type = [_achiTypeArray objectAtIndex:[indexPath section]];
        ASAchievementDetailVC *achiVC = [[ASAchievementDetailVC alloc] initWithAchiTypeId:type.typeId typeName:type.typeName];
        achiVC.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:achiVC animated:YES];
    }
    else if (indexPath.section == 3)
    {
        // 奖惩
        ASAchievementType* type = [_achiTypeArray objectAtIndex:[indexPath section]];
        ASMyHonorVC* myHonorVC = [[ASMyHonorVC alloc] initWithAchiTypeId:type.typeId typeName:type.typeName];
        myHonorVC.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:myHonorVC animated:YES];
    }
    else if (indexPath.section == 4)
    {
        // 科研
        ASAchievementType* type = [_achiTypeArray objectAtIndex:[indexPath section]];
        //ASResearchVC* researchVC = [[ASResearchVC alloc] initWithAchiTypeId:type.typeId typeName:type.typeName];
        //researchVC.hidesBottomBarWhenPushed = YES;
        //[self.navigationController pushViewController:researchVC animated:YES];
        
        // 此处修改进入后先展示科研的类型
        ASResearchTypeVC *tVC = [[ASResearchTypeVC alloc] initWithAchiTypeId:type.typeId typeName:type.typeName];
        tVC.hidesBottomBarWhenPushed = YES;
        //tVC.delegate = self;
        [self.navigationController pushViewController:tVC animated:YES];
    }
//    else if (indexPath.section == 5)
//    {
//        // 其它
//        ASAchievementType* type = [_achiTypeArray objectAtIndex:[indexPath section]];
//        ASAchievementDetailVC *achiVC = [[ASAchievementDetailVC alloc] initWithAchiTypeId:type.typeId typeName:type.typeName];
//        achiVC.hidesBottomBarWhenPushed = YES;
//        [self.navigationController pushViewController:achiVC animated:YES];
//    }
//    else if (indexPath.section == 6)
//    {
//        // 成长轨迹
//        ASAchievementType* type = [_achiTypeArray objectAtIndex:[indexPath section]];
//        ASAchievementDetailVC *achiVC = [[ASAchievementDetailVC alloc] initWithAchiTypeId:type.typeId typeName:type.typeName];
//        achiVC.hidesBottomBarWhenPushed = YES;
//        [self.navigationController pushViewController:achiVC animated:YES];
//    }
}

@end




